
-module(chat_queue_test).

-include_lib("eunit/include/eunit.hrl").

-export([run_tests/0]).


run_tests() ->
    ?debugMsg("chat_queue_test start --"),

    MsgAmount = 200,
    MsgList = [{Id, "msg " ++ integer_to_list(Id)} || Id <- lists:seq(1, MsgAmount)],

    Key = test_key,
    Max = 100,
    Amount = 10,
    Data0 = chat_queue:get_queue(Key),
    F = fun(Msg, Data) ->
            chat_queue:in(Data, Msg, Max)
        end,
    Data1 = lists:foldl(F, Data0, MsgList),

    chat_queue:set_queue(<<"ignore">>, {Max, Data1}),
    {Max, Data1} = chat_queue:get_queue(<<"ignore">>),

    %?assert(hd(chat_queue:fetch(Key, 0, Amount)), {191,"msg 191"}),
    ?debugMsg("chat_queue_test queue fetch"),
    ?debugVal(chat_queue:fetch(Data1, 0, Amount)),
    ?debugVal(chat_queue:fetch(Data1, 150, Amount)),
    ?debugVal(chat_queue:fetch(Data1, 105, Amount)),
    ?debugVal(length(chat_queue:fetch(Data1, 105, Amount))),
    ?debugVal(length(chat_queue:fetch(Data1, 80, Amount))),

    ?debugMsg("chat_queue_test queue reset"),
    {_Id, Data2} = chat_queue:reset_queue(Data1, 1),
    ?debugVal(chat_queue:fetch(Data2, 0, Amount)),
    ?debugVal(chat_queue:fetch(Data2, 105, Amount)),
    ?debugVal(length(chat_queue:fetch(Data2, 105, Amount))),
    ?debugVal(chat_queue:fetch(Data2, 80, Amount)),
    ?debugVal(length(chat_queue:fetch(Data2, 80, Amount))),

    ?debugMsg("chat_queue_test over"),
    ok.

